using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace Report.PhieuB
{
    public partial class UcB61 : DevExpress.XtraEditors.XtraUserControl
    {
        public UcB61()
        {
            InitializeComponent();
        }

        private void UcB61_Load(object sender, EventArgs e)
        {
            DataTable dtB61 = new DsReportX.Table3ColDataTable();
            dtB61 = CalculateData(dtB61);
            gridControlA.DataSource = dtB61;
        }

        DataTable CalculateData(DataTable dt)
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            SqlCommand cmd = con.CreateCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            try
            {
                con.Open();
                cmd.CommandText = "SELECT *, Number1+Number2 Number3 FROM \n" +
                                    "( SELECT COUNT(CauTraLoiFIX1) Number1 FROM Phieu A INNER JOIN ChiTietPhieu B ON A.ID = B.PhieuID \n" +
                                    "WHERE A.LoaiPhieu = 'PhieuB' AND MaCau = 17 AND CauTraLoiFIX1 = 1 \n" +
                                    ") DuocTuyenTruyen, ( \n" +
                                    "SELECT COUNT(CauTraLoiFIX1) Number2 FROM Phieu A INNER JOIN ChiTietPhieu B ON A.ID = B.PhieuID \n" +
                                    "WHERE A.LoaiPhieu = 'PhieuB' AND MaCau = 17 AND CauTraLoiFIX1 = 2 \n" +
                                    ") KDuocTuyenTruyen";
                da.SelectCommand = cmd;
                da.Fill(dt);

                DataRow r = dt.NewRow();
                r["Number1"] = Math.Round(Convert.ToDecimal(dt.Rows[0]["Number1"].ToString()) * 100 / Convert.ToDecimal(dt.Rows[0]["Number3"].ToString()), 4);
                r["Number2"] = Math.Round(Convert.ToDecimal(dt.Rows[0]["Number2"].ToString()) * 100 / Convert.ToDecimal(dt.Rows[0]["Number3"].ToString()), 4);
                dt.Rows.Add(r);

                return dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
            finally
            {
                con.Close();
            }
        }
    }
}
